package com.classtest.DemoExpend.controller;

import com.alibaba.fastjson.JSON;
import com.classtest.DemoExpend.bean.DbGoods;
import com.classtest.DemoExpend.bean.DbUser;
import com.classtest.DemoExpend.service.DbShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HomeShopController {

    @Autowired
    DbShopService dbShopService;

    @RequestMapping("/homeShop")
    // 记录商品数量
    public String homeShop(HttpServletRequest request,DbGoods dbGoods) {
        DbUser user = (DbUser) request.getSession().getAttribute("user");
        dbGoods.setUsId(user.getId());
        List<DbGoods> userShopCount = dbShopService.getShopCount(JSON.toJSONString(dbGoods));
        int count = 0; // 记录该用户下的商品数量
        Map<String,Object> shopResult = new HashMap<>();
        for (DbGoods good:userShopCount) {
            count ++;
        }
        shopResult.put("count",count);
        return JSON.toJSONString(shopResult);
    }


}
